import java.util.Arrays;

public class MyStack {
    public int usedSize;
    public int[] elem;

    public MyStack(int[] elem) {
        this.elem = new int[10];
    }

    //压栈
    public void push(int val) {
        if(isFull()) {
            //扩容
            elem = Arrays.copyOf(elem,2 * elem.length);
        }
        elem[usedSize - 1] = val;
        usedSize++;
    }
    private boolean isFull() {
        return usedSize == elem.length;
    }

    //出栈
    public int pop() {
        if(isEmpty()) {
            throw new EmptyException("栈是空的！");
        }
        /*int val = elem[usedSize-1];
        usedSize--;
        return val;*/
       /* usedSize--;
        return elem[usedSize];*/
        return elem[--usedSize];
    }
    public boolean isEmpty() {
        return usedSize == 0;
    }
    public int peek() {
        if(isEmpty()) {
            throw new EmptyException("栈是空的！");
        }
        return elem[usedSize-1];
    }
}
